package com.shop.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.ex.ShoppingException;
import com.shop.feignClient.DiscountClient;
import com.shop.po.Discount;
import com.shop.user.Interceptor.GetUserInfoInterceptor;
import com.shop.user.mapper.DiscountUserMapper;
import com.shop.user.po.DiscountUser;
import com.shop.user.po.User;
import com.shop.user.po.dto.DiscountDto;
import com.shop.user.service.DiscountUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author banxia
 * @since 2023-02-27
 */
@Service
public class DiscountUserServiceImpl extends ServiceImpl<DiscountUserMapper, DiscountUser> implements DiscountUserService {

    @Autowired
    private DiscountUserMapper discountUserMapper;

    @Autowired
    private DiscountClient discountClient;

    SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public List<DiscountDto> getDiscountDtoList(Boolean expire,Boolean used,Boolean notStart){
        User user = GetUserInfoInterceptor.getUser();
        if (user == null) {
            ShoppingException.ThrowShoppingException("请先登录");
        }
        String now = dateFormat.format(new Date());
        List<DiscountDto> res=discountUserMapper.getDiscountDtoList(expire,used,notStart,user.getId(),now);
        return res;
    }

    @Transactional
    @Override
    public boolean robDiscount(Long discountId) {
        User user = GetUserInfoInterceptor.getUser();
        if (user==null){
            ShoppingException.ThrowShoppingException("请先登录");
        }
        //查询其信息
        Discount discount = discountClient.getDiscountById(discountId);
        if (discount == null) {
            ShoppingException.ThrowShoppingException("该优惠券不存在");
        }
        //判断是否已经抢过了。。
        LambdaQueryWrapper<DiscountUser> wrapper=new LambdaQueryWrapper();
        wrapper.eq(DiscountUser::getUserId,user.getId());
        DiscountUser one = discountUserMapper.selectOne(wrapper);
        if (one != null) {
            ShoppingException.ThrowShoppingException("该用户已经拥有过该优惠券了，每个人仅限领一张");
        }
        DiscountUser discountUser=new DiscountUser();
        discountUser.setUserId(user.getId());
        discountUser.setDiscountId(discountId);
        discount.setCount(1L);
        int i = discountUserMapper.insert(discountUser);
        //user_id,discount_id联合主键设置为唯一，确保了用户只能领取一张优惠券！
        if (i == 0) {
            ShoppingException.ThrowShoppingException("添加优惠券异常");
        }
        //最后再调用Feign，若Feign失败其余全部回滚！！，防止分布式事务的无法达到最终一致性
        //没有则尝试抢该优惠券，直接更新discount的库存，判断更新成功与否
        boolean b = discountClient.changDiscountCount(discountId, -1L);
        if (!b){
            ShoppingException.ThrowShoppingException("优惠券已被领完~~~~");
        }
        return true;
    }
}
